﻿/* Copyright 2012 Sannel Software, L.L.C.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at

 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */
using System;
using System.Collections.Generic;
using System.Text;

namespace Sannel.Calculate.Nodes
{
	/// <summary>
	/// Represents a collection of nodes that should be resolved before doing operators on this node.
	/// </summary>
	public class SectionNode : BaseNode
	{
		/// <summary>
		/// The first node of the list
		/// </summary>
		public BaseNode FirstNode
		{
			get;
			protected set;
		}

		/// <summary>
		/// The last node added to the list
		/// </summary>
		public BaseNode LastNode
		{
			get;
			protected set;
		}

		/// <summary>
		/// Always returns NodeType.Section;
		/// </summary>
		public override NodeType NodeType
		{
			get { return NodeType.Section; }
		}

		/// <summary>
		/// Copys this node and all its subnodes into a new SectionNode
		/// </summary>
		/// <returns></returns>
		public override BaseNode Copy()
		{
			SectionNode node = new SectionNode();
			BaseNode currentNode = FirstNode;
			while (currentNode != null)
			{
				node.AppendChildNode(currentNode.Copy());
				currentNode = currentNode.Next;
			}

			return node;
		}

		/// <summary>
		/// Appends a node to the end of this list.
		/// </summary>
		/// <param name="node"></param>
		public void AppendChildNode(BaseNode node)
		{
			if (node == null)
			{
				throw new ArgumentNullException("node");
			}
			node.RemoveFromList(); // make sure the node has been removed from its existing list
			node.Parent = this;
			if (LastNode == null)
			{
				FirstNode = node;
			}
			else
			{
				LastNode.InsertNext(node);
			}

			LastNode = node;
		}

		internal void MoveNextIfLast(BaseNode currentNode)
		{
			if (currentNode == null)
			{
				throw new ArgumentNullException("currentNode");
			}

			if (LastNode == currentNode && LastNode.Next != null)
			{
				LastNode = LastNode.Next;
			}
		}

		internal void MovePreviousIfFirst(BaseNode currentNode)
		{
			if (currentNode == null)
			{
				throw new ArgumentNullException("currentNode");
			}

			if (FirstNode == currentNode && FirstNode.Previous != null)
			{
				FirstNode = FirstNode.Previous;
			}
		}

		internal void MovePreviousIfLast(BaseNode currentNode)
		{
			if (currentNode == null)
			{
				throw new ArgumentNullException("currentNode");
			}

			if (LastNode == currentNode)
			{
				LastNode = currentNode.Previous;
			}
		}

		internal void MoveNextIfFirst(BaseNode currentNode)
		{
			if (currentNode == null)
			{
				throw new ArgumentNullException("currentNode");
			}

			if (FirstNode == currentNode)
			{
				FirstNode = currentNode.Next;
			}
		}

		internal void ReplaceWithIfFirstOrLast(BaseNode currentNode, BaseNode replacement)
		{
			if (currentNode == null)
			{
				throw new ArgumentNullException("currentNode");
			}

			if (replacement == null)
			{
				throw new ArgumentNullException("replacement");
			}

			if (FirstNode == currentNode)
			{
				FirstNode = replacement;
			}

			if (LastNode == currentNode)
			{
				LastNode = replacement;
			}
		}
	}
}
